// Public method for invoking an update of the search results
var runSearch;
// Public parameters for setting the current page and page limit
var perPage = 16;
var curPage = 1;
(function($) {
// Form containing search query parameters
var target = "#illustratorSearchForm";
// Path to the search API back-end
var searchTarget = "/search-api/search/illustrators";
// Page area to render results to
var renderTarget = ".illustrator-result-list";
// Load search results into the UI
function populateSearchResults(data) {
// Remove previous search results
$(renderTarget + ' .search-result').remove();
if (data.success) {
if (data.data.length) {
// Load new search results into the UI
for (var key = 0; key < perPage && key < data.data.length; key++) {
var record = data.data[key];
$(renderTarget).append("
" + record.cached_content + "");
}
$('.page-selector').hide();
// The API returns perPage+1 records if possible - if it doesn't, it means there are no more pages of data
// and the next selector should not be shown
if (data.data.length > perPage) {
$('.page-next').show();
}
if (curPage != 1) {
$('.page-prev').show();
}
} else {
$(renderTarget).append("No Results Found");
}
}
}
function unblockSearch() {
$('.whole-search-page').unblock();
}
/**
* Executes a search and populates the results area with results.
* The form and results area will be blocked on the UI until the API call completes.
*/
runSearch = function() {
// User-entered parameters
var params = {
illustrator_name: $(target).find('#illustrator_name').val(),
style_id: $(target).find('#art_style').val(),
medium_id: $(target).find('#art_mediums').val(),
topics: $(target).find('#illustration_topic').val(),
region_id: $(target).find('#region').val(),
pageLimit: perPage,
currentPage: curPage
}
// Transition from the landing page view to the search results view
$('.search-page-results').show();
$('.search-page-landing').hide();
$('.whole-search-page').block();
$.ajax({
url: searchTarget,
data: params,
success: populateSearchResults,
complete: unblockSearch,
});
}
$(function() {
// Execute a search when the search form is submitted
$(target).bind('submit', function(ev) {
ev.preventDefault();
runSearch();
return false;
});
// Update the search results when the number of results per page is changed
$('.pagination-button').bind('click', function(ev) {
ev.preventDefault();
var value = parseInt($(this).text());
if (value) {
$('.pagination-button').removeClass('active');
$(this).addClass('active');
perPage = value;
runSearch();
}
return false;
});
// Update the current page and re-run the search when the page is changed
$('.page-prev').bind('click', function(ev) {
ev.preventDefault();
curPage--;
runSearch();
return false;
});
// Update the current page and re-run the search when the page is changed
$('.page-next').bind('click', function(ev) {
ev.preventDefault();
curPage++;
runSearch();
return false;
});
});
})(jQuery);